<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>index</title>
    <script src="/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <input type="text" @input="search" v-model="value" />
      <ul>
        <li v-for="item in (flag ? preData : data)" :key="item.id">
          {{item.name}} --- {{item.age}}
        </li>
      </ul>
    </div>
    <script>
      Vue.config.productionTip = false;
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            value: "",
            data: [],
            preData: [
              { id: "001", name: "周杰伦", age: "20" },
              { id: "002", name: "林俊杰", age: "21" },
              { id: "003", name: "李荣浩", age: "22" },
            ],
            flag: true,
          };
        },
        methods: {
          search() {
            if (this.value) {
              this.flag = false
            }
            this.data = this.preData.filter((item) => {
              return item.name.indexOf(this.value) + 1;
            });
          },
        },
      });
    </script>
  </body>
</html>
